# Maintainer: Daniel M. Capella <polyzen@archlinux.org>

_name=svelte-language-tools
pkgname=svelte-language-server
pkgver=0.16.1
pkgrel=1
pkgdesc='Language server for Svelte'
arch=('any')
url=https://github.com/sveltejs/language-tools/tree/master/packages/language-server
license=('MIT')
depends=('nodejs' 'typescript')
makedepends=('git' 'npm' 'pnpm')
conflicts=("nodejs-$pkgname")
replaces=("nodejs-$pkgname")
source=("$_name::git+https://github.com/sveltejs/language-tools.git#tag=language-server-$pkgver")
b2sums=('SKIP')

prepare() {
  cd $_name
  pnpm install --frozen-lockfile
}

build() {
  cd $_name/packages/svelte2tsx
  pnpm build
  cd ../language-server
  pnpm build
}

check() {
  cd $_name/packages/language-server
  pnpm test
}

package() {
  local mod_dir=/usr/lib/node_modules/$pkgname
  install -d "$pkgdir"/{usr/bin,$mod_dir}
  ln -s $mod_dir/bin/server.js "$pkgdir"/usr/bin/svelteserver

  cd $_name
  install -Dm644 -t "$pkgdir"/usr/share/licenses/$pkgname LICENSE

  cd packages/language-server
  # Prune unnecessary packages
  cp package.json{,.bak}
  sed -i 's/workspace:~/*/' package.json
  rm -r node_modules
  npm install --production
  rm -r node_modules/typescript
  mv package.json{.bak,}

  cp -r bin dist node_modules package.json "$pkgdir"/$mod_dir
  install -Dm644 -t "$pkgdir"/usr/share/doc/$pkgname README.md
}
